/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Dao;

import Controler.CXuLy;
import Model.Pojo.Dondathang;
import Model.Pojo.Parameters;
import Model.Util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class DAODonHang
{

    /**
     * Them don hang
     *
     * @param dtoDH
     * @throws Exception
     */
    public void InsertDonHang(Dondathang dtoDH) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();

        //Kiem tra don hang co trong database hay khong?

        //Insert vao database neu chua co don hang
        DAOProvider.Insert(session, dtoDH);

        session.close();
    }

    /**
     * update thong tin don hang
     *
     * @param dtoDH
     * @throws Exception
     */
    public void UpdateDonHang(Dondathang dtoDH) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();

        //Kiem tra don hang co trong database hay khong?

        //Insert vao database neu chua co don hang
        DAOProvider.Update(session, dtoDH);

        session.close();
    }

    /**
     * Lay danh sach don hang
     *
     * @param tinhtrang
     * @return
     */
    public List LayDanhSachDonHang(boolean tinhtrang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Dondathang ddh where ddh.tinhtrang =:tinhtrang";

        try
        {
            return DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("tinhtrang", tinhtrang)
            });
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Chi tiet don hang
     *
     * @param id_DonHang
     * @return
     */
    public Dondathang ChiTietDonHang(int id_DonHang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Dondathang ddh where ddh.id =:id_DonHang";

        try
        {
            List query = DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("id_DonHang", id_DonHang)
            });
            return query != null ? (Dondathang) query.get(0) : null;
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Xoa don hang
     */
    public void XoaDonHang(int id_DonHang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        try
        {
            DAOProvider.Delete(session, DAOProvider.
                    SelectOne(session, Dondathang.class, id_DonHang));
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    public List TimKiemDonHang(String madonhang, String makhachhang, String[] date) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        String hql = "";
        if (date.length != 2)
        {
            hql = "from Dondathang ddh inner join fetch ddh.khachhang kh ";
            hql += "where (ddh.madonhang=:madonhang or kh.makh=:makhachhang)";
            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("madonhang", madonhang), new Parameters("makhachhang",
                                                                           makhachhang)
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }
        else
        {
            hql = "from Dondathang ddh inner join fetch ddh.khachhang kh ";
            hql += "where (ddh.madonhang=:madonhang or kh.makh=:makhachhang) and ddh.ngaythanhtoan between :date1 and :date2";
            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("madonhang", madonhang), new Parameters("makhachhang",
                                                                           makhachhang),
                    new Parameters("date1",
                                   CXuLy.ConvertStringToDate(date[0])), new Parameters("date2",
                                                                                       CXuLy.
                    ConvertStringToDate(date[1]))
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }

    }
}
